﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Publish_ProductDetail : System.Web.UI.Page
{
    protected string m_title = "";
    protected string m_detail = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = "";
        if (Request.Params["id"] != null)
            id = Request.Params["id"].ToString();
        GssDO.DOProduct DO = getById(id);
        if (DO != null && DO.IsDeleted == 0)
        {
            m_title = DO.Title;
            m_detail = ShowDetail(DO);
            lstProduct.DataSource = GenerateRandom(DO.ProductCategoryID, 6, DO.PKIDProduct);
            lstProduct.DataBind();

            DataTable dt = GetCommentByProduct(DO.PKIDProduct);
            if (dt != null && dt.Rows.Count > 0)
            {
                rptComment.DataSource = dt;
                rptComment.DataBind();
            }
        }
        else
            Response.Redirect(@"Shop.aspx", false);
    }

    private string ShowDetail(GssDO.DOProduct DO)
    {
        string rs = "";

        rs = "<p>";
        if (DO.Image != "")
        {
            rs += "<table align='left'><tr><td style='padding: 0px 10px 2px 0px;'>";
            rs += "<img alt='" + DO.Title.Trim() + "' src='../Photos/Product/" + DO.Image + "' border='0' align='left' onerror=\"this.src='../images/noimage90.gif'\" />";
            rs += "</td></tr></table>";
        }
        if (DO.Title.Trim() != "")
        {
            rs += "<h3>" + DO.Title.Trim();
            if (DO.ProductType == 1)
                rs += " <img alt='' border='0' class='icon' src='../images/sale.gif' />";
            else if (DO.ProductType == 2)
                rs += " <img alt='' border='0' class='icon' src='../images/new.gif' />";
            else if (DO.ProductType == 3)
                rs += " <img alt='' border='0' class='icon' src='../images/hot.gif' />";
            rs += "</h3>";
        }
        double p = DO.Price, r = DO.Price - DO.SaleOff;
        if (p != 0)
        {
            if (r != 0 && r < p)
                rs += "<s>" + p.ToString("##,###") + " VNĐ</s><br>";
            rs += "<span class='price'>" + r.ToString("##,###") + " VNĐ</span><br>";
        }
        else if (p == 0)
            rs += "<span class='price'>(Giá liên hệ)</span><br>";
        GssDO.DOProductCategory category = getCategoryById(DO.ProductCategoryID);
        if (category != null && category.IsDeleted == 0)
        {
            rs += "<span class='name'>Danh mục:</span>";
            string s = "<a class='link' href='Product.aspx?id=" + category.PKIDCategory + "'>" + category.Title.Trim() + "</a>";
            if (category.ParentID != "0")
            {
                category = getCategoryById(category.ParentID);
                if (category != null && category.IsDeleted == 0)
                    rs += "<a class='link' href='Product.aspx?id=" + category.PKIDCategory + "'>" + category.Title.Trim() + "</a> &raquo; ";    
            }
            rs += s;
            rs += "<br>";   
        }
        GssDO.DOShop shop = getShopById(DO.ShopID);
        if (shop != null && shop.IsDeleted == 0)
        {
            rs += "<span class='name'>Shop:</span>";
            rs += "<a class='link' href='ShopDetail.aspx?id=" + shop.PKIDShop + "'>" + shop.Title.Trim() + "</a>";
            rs += "<br>";
        }
        //if (DO.SlideShow != "")
            rs += "Xem slide-show ";
        rs += "<a class='link' href=\"javascript:ShowComment('" + DO.PKIDProduct + "');\">Viet nhan xet</a></p>";
        rs += "<p><br></p>";
        rs += "<p>" + DO.Description.Trim() + "</p>";

        return rs;
    }

    protected string BindProduct(string id)
    {
        string rs = "";
        GssDO.DOProduct DO = getById(id);
        if (DO != null && DO.IsDeleted == 0)
        {
            double p = Convert.ToDouble(DO.Price), r = Convert.ToDouble(DO.Price - DO.SaleOff);
            // type=1: sales off; type=2: new arrival; type=3 hot product
            if (DO.Image != "")
            {
                rs += "<table align='center'><tr><td style='padding: 4px;'><a title='" + DO.ShortText + "' href='ProductDetail.aspx?id=" + id + "'>";
                rs += "<img alt='" + DO.ShortText + "' src='ProductThumbnail.aspx?id=" + id + "&width=110' border='0' align='left' onerror=\"this.src='../images/noimage90.gif'\" />";
                rs += "</a></td></tr></table>";
            }
            rs += "<p class='title'><a title='" + DO.ShortText + "' href='ProductDetail.aspx?id=" + id + "'>" + DO.Title + "</a>";
            if (DO.ProductType == 1)
                rs += " <img alt='' border='0' class='icon' src='../images/sale.gif' /></p>";
            else if (DO.ProductType == 2)
                rs += " <img alt='' border='0' class='icon' src='../images/new.gif' /></p>";
            else if (DO.ProductType == 3)
                rs += " <img alt='' border='0' class='icon' src='../images/hot.gif' /></p>";
            else
                rs += "</p>";
            if (p != 0)
            {
                if (r != 0 && r < p)
                {
                    rs += "<p class='price'><s>" + p.ToString("##,###") + " VNĐ</s><br><span>" + r.ToString("##,###") + " VNĐ</span></p>";
                }
                else
                    rs += "<p class='price'><span>" + p.ToString("##,###") + " VNĐ</span></p>";
            }
            else if (p == 0)
                rs += "<p class='price'>Giá liên hệ</p>";
        }
        return rs;
    }

    protected string BindComment(object fullname, object email, object isEmail, object createDate, object title, object content)
    {
        string rs = "";
        int isShow = 0;
        DateTime date = Convert.ToDateTime(createDate);
        try
        {
            isShow = Convert.ToInt32(isEmail);
        }
        catch
        {
            isShow = 0;
        }
        rs += "<p class='title'>" + title + "</p>";
        rs += "<p>" + content.ToString() + "</p>";
        rs += "<p class='author'>";
        rs += "Người gửi: <span class='name'>" + fullname.ToString() + "</span><br>";
        if (isShow==1)
            rs += "Địa chỉ email: <span class='name'>" + email.ToString() + "</span><br>";
        rs += "Ngày gửi: <span class='name'>" + date.ToString("dd/MM/yyyy") + "</span>";
        rs += "</p>";

        return rs;
    }

    private DataTable GenerateRandom(string category, int rows, string other)
    {
        DataTable dt = GetListOfOtherProducts(category, 0, other);
        if (dt != null)
        {
            Random rnd = new Random();
            int max = dt.Rows.Count;
            foreach (DataRow dr in dt.Rows)
            {
                dr["ProductType"] = rnd.Next(0, max * max);
            }
            DataRow[] drs = dt.Select("IDProduct <> '' ", "ProductType");

            DataTable dtNews = new DataTable();
            dtNews.Columns.Add(new DataColumn("IDProduct"));
            for (int i = 0; i < ((drs.Length > rows) ? rows : drs.Length); i++)
            {
                DataRow dr = dtNews.NewRow();
                dr["IDProduct"] = drs[i]["IDProduct"];
                dtNews.Rows.Add(dr);
            }
            return dtNews;
        }
        return null;
    }

    private DataTable GetListOfOtherProducts(string category, int rows, string other)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            dt = BL.getProductByCategory(category, 0, other);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }

    private DataTable GetCommentByProduct(string id)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLComment BL = new GssBL.BLComment();
            dt = BL.getCommentByProduct(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }

    private GssDO.DOProduct getById(string id)
    {
        GssDO.DOProduct DO = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            DO = BL.getProductByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }

    private GssDO.DOProductCategory getCategoryById(string id)
    {
        GssDO.DOProductCategory DO = null;
        try
        {
            GssBL.BLProductCategory BL = new GssBL.BLProductCategory();
            DO = BL.getProductCategoryByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }

    private GssDO.DOShop getShopById(string id)
    {
        GssDO.DOShop DO = null;
        try
        {
            GssBL.BLShop BL = new GssBL.BLShop();
            DO = BL.getShopByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }
}
